<?php
/**
 * Created by PhpStorm.
 * User: server
 * Date: 2019/5/25
 * Time: 10:40
 */

namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path'), EXT);

use app\admin\Controller;
use think\Db;
use think\Config;
use think\Paginator;
use think\Request;
use think\Validate;

class DispatchInsurance extends Controller
{

    use \app\admin\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {

        $data = input();
        $map = [];
//        $map['di.status'] = 0;

        if (!empty($data['group_id'])) {
            $map['di.group_id'] = $data['group_id'];
        }
        if (!empty($data['title'])) {
            $map['r.title'] = ['like', "%{$data['title']}%"];
        }
        if (!empty($data['realname'])) {
            $map['au.realname'] = ['like', "%{$data['realname']}%"];
        }
        if (!empty($data['id_card'])) {
            $map['d.id_card'] = ['like', "%{$data['id_card']}%"];
        }
        if (!empty($data['mobile'])) {
            $map['au.mobile'] = ['like', "%{$data['mobile']}%"];
        }

        $listRows = $this->request->param('numPerPage') ? 20 : Config::get("paginate.list_rows");

        $list = Db::name('dispatch_insurance')
            ->alias('di')
            ->join('tp_dispatch_traveler_group dtg', 'dtg.id=di.group_id', 'left')
            ->join('tp_route r', 'r.id=dtg.route_id', 'left')
            ->join('tp_driver d', 'd.user_id=dtg.driver_user_id', 'left')
            ->join('tp_admin_user au', 'au.id=dtg.driver_user_id', 'left')
            ->field('di.*,au.realname,dtg.route_id,dtg.driver_user_id,dtg.go_date,dtg.end_date,r.title,d.id_card,au.mobile,au.sex,d.birthday')
            ->where($map)
            ->order('id desc')
            ->paginate($listRows, false, ['query' => $this->request->get()]);

        $new_list = $list->toArray()['data'];

        foreach ($new_list as $k => $v) {
            $new_list[$k]['dispatch_traveler'] = Db::name('order_traveler')
                ->where('group_id', $v['group_id'])
                ->select();
        }

        $this->view->assign('list', $new_list);
        $this->view->assign("count", $list->total());
        $this->view->assign("page", $list->render());
        $this->view->assign('numPerPage', $list->listRows());
        return $this->view->fetch();
    }

    /**
     * 保存出行人员的保险金额
     * @return mixed
     */
    public function save_traveler()
    {
        $data = input();

        Db::startTrans();
        try{
            // 修改司机的保险金额
            Db::name('dispatch_insurance')
                ->where('id', $data['id'])
                ->update([
                    'insurance_money' => $data['p_insurance_money'],
                    'remark' => $data['remark'],
                    'status' => 1
                ]);

            // 修改出行人员保险金额
            foreach ($data['insurance_money'] as $k => $v) {
                Db::name('order_traveler')
                    ->where('id', $k)
                    ->setField('insurance_money', $v);
            }
            // 提交事务
            Db::commit();
            return ajax_return_adv('保存成功');
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ajax_return_adv_error('保存失败');
        }
    }
}